﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading;

namespace LvDevice
{
    class Program
    {
        static void Main(string[] args)
        {
            Device d = new Device();
            d.Link();
        }
    }



    class Device
    {
        public async void Link()
        {
            TcpClient client = new TcpClient();
            while (true)
            {
               

                if (!client.Connected)
                {
                    client.Connect("localhost", 22000);
                    var heartbeat = Encoding.UTF8.GetBytes(JObject.FromObject(new { heartbeat = 0 }).ToString());
                    await client.GetStream().WriteAsync(heartbeat, 0, heartbeat.Length);
                }
                else
                {
                    var heartbeat= Encoding.UTF8.GetBytes(JObject.FromObject(new {heartbeat = 0}).ToString());
                    await client.GetStream().WriteAsync(heartbeat, 0, heartbeat.Length);
                }
                //await Task.Delay(1000);
                Thread.Sleep(2000);
            }
           
          



        }
    }
}
